//116 tutorial.
//http://www.youtube.com/watch?NR=1&v=WKf5CvuQwAQ
//

package databases;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import bookless.pakage.User;

public class UserDB {

	public static final String KEY_ROWID = "_id";
	public static final String KEY_NAME = "user_name";
	public static final String KEY_PASSWORD = "user_password";

	private static final String DATABASE_NAME = "userDB";
	private static final String DATABASE_TABLE = "userTable";
	private static final int DATABASE_VERSION = 1;

	private DbHelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;

	private static class DbHelper extends SQLiteOpenHelper {

		public DbHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		// this method creates our database! this is only called once.
		@Override
		public void onCreate(SQLiteDatabase db) {

			// TODO Auto-generated method stub
			// creating the database table. specifying the table name and
			// different variables in the table.
			db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
					+ " TEXT NOT NULL, " + KEY_PASSWORD + " TEXT NOT NULL);");
		}

		// if oncreate() is called, onupgrade() is called instead.
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);

		}
	}

	public UserDB(Context c) {
		ourContext = c;
	}

	public UserDB open() throws SQLException {
		ourHelper = new DbHelper(ourContext);
		ourDatabase = ourHelper.getWritableDatabase();
		return this;
	}

	// closes SQLiteOpenHelper
	public void close() {
		ourHelper.close();
	}

	public long createEntry(String name, String password) {
		// TODO Auto-generated method stub
		ContentValues cv = new ContentValues();
		cv.put(KEY_NAME, name);
		cv.put(KEY_PASSWORD, password);
		// insert data into database
		return ourDatabase.insert(DATABASE_TABLE, null, cv);
	}

	public String getData() {
		// TODO Auto-generated method stub
		String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_PASSWORD };

		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null); // possible to enter other info in null

		String result = "";

		int iRow = c.getColumnIndex(KEY_ROWID);
		int iName = c.getColumnIndex(KEY_NAME);
		int iPassword = c.getColumnIndex(KEY_PASSWORD);

		// loop that iterates through the db list.
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			result = result + c.getString(iRow) + " User name: " + c.getString(iName)
					+ " Password: " + c.getString(iPassword) + "\n";

		}
		return result;
	}
	
	public long getUserId(String userName){
		long id = 0;
		
		String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_PASSWORD };       
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		int rowId = c.getColumnIndex(KEY_ROWID);
		int name = c.getColumnIndex(KEY_NAME);
		c.moveToPosition(1);
		c.moveToFirst();
		
		while(!c.isAfterLast()){
			String tmp = c.getString(name);
			
			if( tmp.equals(userName)){
				id = c.getLong(rowId);
				break;
				}
			c.moveToNext();
		}
		
		return id;
		
	}

	public User getUserfromDB(String userName){
		
		String uName = "";
		String pword = "";
		
		String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_PASSWORD };       
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		int pwordCol = c.getColumnIndex(KEY_PASSWORD);
		int name = c.getColumnIndex(KEY_NAME);
		
		c.moveToPosition(1);
		c.moveToFirst();
		
		while(!c.isAfterLast()){
			String tmp = c.getString(name);
			
			if( tmp.equals(userName)){
				uName = userName;
				pword = c.getString(pwordCol);
				break;
				}
			c.moveToNext();
		}
		
		return new User(uName, pword);
		
	}

	public void removeUser(String session) {
		
		ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=\'" + session + "\'", null);
		
	}

	public void removeAllUsers() {
		
        ourDatabase.delete(DATABASE_TABLE, null, null);
	}
}